import React from 'react';
import {Table} from 'antd';
import PropTypes from 'prop-types';
import routes from './routes';
import {TABLE, PAGINATION_OPTIONS, FIXED_COLUMN_WIDTH, COMMON_TABLE_QUERY_FAIL_TEXT, COMMON_TABLE_EMPTY_TEXT, DATETIME_FORMAT} from '../constants';
import {Link} from 'react-router-dom';
import {hasPermissions} from './utils';
import {partContractInvoiceStatus} from '../Enum';
import {conventEnumValueToString, formatDateTime} from '../utils';
import {PERMISSIONS} from './constants';
import DropdownMenu from 'Shared/components/DropdownMenu';
import {injectIntl} from './intl';
import Ellipsis from 'ant-design-pro/lib/Ellipsis';
export class TablePanel extends React.PureComponent {
    constructor(props) {
        super(props);
        this.state = {
            loading: false,
        };
    }

    onChangeSort = (pagination, filters, sorter) => {
        this.props.onChangeSort({
            pageIndex: pagination.current - 1,
            pageSize: pagination.pageSize,
            sortedColumnKey: sorter.columnKey,
            sortedOrder: sorter.order,
        });
    };

    render() {
        const {formatMessage} = this.props.intl;
        const {pageIndex, pageSize, total, sortedColumnKey, sortedOrder, permissions} = this.props;
        const pagination = {
            total,
            pageSize,
            current: pageIndex + 1,
            ...PAGINATION_OPTIONS
        };
        const locale = {
            emptyText: this.props.errorMessage ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        return (
            <div>
                <Table className="white-space-nowrap"
                    dataSource={this.props.data}
                    rowKey="id"
                    pagination={pagination}
                    loading={this.props.loading || this.state.loading}
                    onChange={this.onChangeSort}
                    locale={locale}
                    {...TABLE} >
                    {!this.props.isDealerUser &&
                        <Table.Column title={formatMessage({
                            id: 'tablePanel.text.dealer',
                            defaultMessage: '总代'
                        })}
                        dataIndex="dealerCode"
                        sorter="true"
                        sortOrder={sortedColumnKey === 'dealerCode' && sortedOrder}
                        render={(text, record) => `(${record.dealerCode})${record.dealerName}`}>
                        </Table.Column>
                    }
                    <Table.Column title={formatMessage({
                        id: 'tablePanel.text.brand',
                        defaultMessage: '品牌'
                    })}
                    dataIndex="brandName"
                    sorter="true"
                    sortOrder={sortedColumnKey === 'brandName' && sortedOrder}
                    render={(text, record) => `(${record.brandCode})${record.brandName}`}>
                    </Table.Column>

                    <Table.Column title={formatMessage({
                        id: 'tablePanel.text.code',
                        defaultMessage: '发运单号'
                    })}
                    dataIndex="code"
                    sorter="true"
                    sortOrder={sortedColumnKey === 'code' && sortedOrder}
                    render={(text, record) => <Link key="detail" to={routes.detail.format(record.partShipmentId)}>{text}</Link>}>
                    </Table.Column>

                    <Table.Column title={formatMessage({
                        id: 'tablePanel.text.partInvoiceCode',
                        defaultMessage: '发票号'
                    })}
                    dataIndex="partInvoiceCode"
                    sorter="true"
                    sortOrder={sortedColumnKey === 'partInvoiceCode' && sortedOrder}>
                    </Table.Column>

                    <Table.Column title={formatMessage({
                        id: 'tablePanel.text.partContractInvoiceStatus',
                        defaultMessage: '发票状态'
                    })}
                    dataIndex="partContractInvoiceStatus"
                    sorter="true"
                    sortOrder={sortedColumnKey === 'partContractInvoiceStatus' && sortedOrder}
                    render={text => conventEnumValueToString(partContractInvoiceStatus, text)}>
                    </Table.Column>

                    <Table.Column title={formatMessage({
                        id: 'tablePanel.text.invoicedDate',
                        defaultMessage: '开票日期'
                    })}
                    dataIndex="invoicedDate"
                    sorter="true"
                    sortOrder={sortedColumnKey === 'invoicedDate' && sortedOrder}
                    render={text => formatDateTime(text, DATETIME_FORMAT)}>
                    </Table.Column>

                    <Table.Column title={formatMessage({
                        id: 'tablePanel.text.partSaleOrderCode',
                        defaultMessage: '订单号'
                    })}
                    dataIndex="partSaleOrderCode"
                    sorter="true"
                    sortOrder={sortedColumnKey === 'partSaleOrderCode' && sortedOrder}
                    render={text => <Ellipsis tooltip fullWidthRecognition length={20}>{text}</Ellipsis>}>
                    </Table.Column>

                    <Table.Column title={formatMessage({
                        id: 'tablePanel.btn.done',
                        defaultMessage: '操作'
                    })}
                    dataIndex="operate"
                    fixed="right"
                    width={FIXED_COLUMN_WIDTH}
                    render={(t, r) => {
                        const menus = [
                            {
                                id: PERMISSIONS.declare,
                                children: <Link key="declare" to={routes.declare.format(r.partShipmentId)}>{formatMessage({
                                    id: 'tablePanel.btn.declare',
                                    defaultMessage: '报关'
                                })}</Link>,
                                hidden: !(hasPermissions(permissions, PERMISSIONS.declare) && hasPermissions(r.options, PERMISSIONS.declare))
                            },
                            {
                                id: PERMISSIONS.clearancet,
                                children: <Link key="clearancet" to={routes.clearancet.format(r.partShipmentId)}>{formatMessage({
                                    id: 'tablePanel.btn.clearancet',
                                    defaultMessage: '清关'
                                })}</Link>,
                                hidden: !(hasPermissions(permissions, PERMISSIONS.clearancet) && hasPermissions(r.options, PERMISSIONS.clearancet))
                            },
                        ];
                        return <DropdownMenu key={r.id} menus={menus} primaryLength={1} id={r.id} />;
                    }}></Table.Column>
                </Table>
            </div>
        );
    }
}

TablePanel.propTypes = {
    data: PropTypes.array.isRequired,
    errorMessage: PropTypes.string.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    intl: PropTypes.object,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number,
    refreshListData: PropTypes.func,
    sortedColumnKey: PropTypes.string,
    sortedOrder: PropTypes.string,
};

import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {
    tableSearch,
} from './actions';
const getData = selectorFactory(['page', 'domainData', 'list', 'data']);
const getPermissions = selectorFactory(['page', 'domainData', 'initData', 'permissions']);
const getIsDealerUser = selectorFactory(['page', 'domainData', 'initData', 'isDealerUser']);
const mapStateToProps = state => ({
    data: getData(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    total: state.getIn(['page', 'domainData', 'list', 'total']),
    errorMessage: state.getIn(['page', 'domainData', 'list', 'message']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySort', 'pageSize']),
    sortedColumnKey: state.getIn(['page', 'appState', 'queryBySort', 'sortedColumnKey']),
    sortedOrder: state.getIn(['page', 'appState', 'queryBySort', 'sortedOrder']),
    isDealerUser: getIsDealerUser(state),
    permissions: getPermissions(state),
});
const mapDispatchToProps = dispatch => ({
    onChangeSort: option => dispatch(tableSearch(option)),
    refreshListData: () => dispatch(tableSearch()),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(TablePanel));
